package View;

import Model.Cliente;
import Model.Produto;
import Control.VendaControl;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class JFrmVenda extends javax.swing.JFrame {

    private Cliente clt = new Cliente(); 
    private VendaControl vControl = new VendaControl();
    private DefaultListModel modelProduto = new DefaultListModel();
    private DefaultListModel modelCarrinho = new DefaultListModel();
   // private ArrayList<Produto> produto = new ArrayList<>();
    
    public JFrmVenda() {
        initComponents();
        atualizaCampo();
    }
    
    public void setCliente(Cliente cliente){
        clt = cliente;
    }
    
    private void atualizaCampo(){
        modelProduto.clear();
        jListProdutos.setModel(modelProduto);
        for(Produto pdt : vControl.produtosEstoque())
            modelProduto.addElement(pdt.getNome());
        jListProdutos.setModel(modelProduto);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLblNomeCliente = new javax.swing.JLabel();
        jBtnCompra = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jListProdutos = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jTxtQtd = new javax.swing.JTextField();
        jBtnAdicionaCompra = new javax.swing.JButton();
        jLblListaProdutos = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListCompra = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Controle Empresa - Venda");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Cliente:");

        jLblNomeCliente.setText("jLabel2");

        jBtnCompra.setText("Concluir Compra");
        jBtnCompra.setEnabled(false);
        jBtnCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCompraActionPerformed(evt);
            }
        });

        jLabel4.setText("Selecione os produtos:");

        jScrollPane3.setViewportView(jListProdutos);

        jLabel3.setText("Quantidade:");

        jBtnAdicionaCompra.setText("Adicionar Compra");
        jBtnAdicionaCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAdicionaCompraActionPerformed(evt);
            }
        });

        jLblListaProdutos.setText("Lista de Produtos");

        jScrollPane2.setViewportView(jListCompra);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(44, 44, 44)
                                .addComponent(jTxtQtd, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jLblNomeCliente))
                            .addComponent(jLabel4)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLblListaProdutos)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(150, 150, 150)
                        .addComponent(jBtnAdicionaCompra))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(159, 159, 159)
                        .addComponent(jBtnCompra)))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLblNomeCliente))
                .addGap(29, 29, 29)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTxtQtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jBtnAdicionaCompra)
                .addGap(23, 23, 23)
                .addComponent(jLblListaProdutos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(jBtnCompra)
                .addContainerGap(42, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        jLblNomeCliente.setText(clt.getNome());
    }//GEN-LAST:event_formWindowOpened

    private void jBtnAdicionaCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAdicionaCompraActionPerformed
        if((jListProdutos.getSelectedIndex()==-1) && (!jTxtQtd.getText().equals(""))){
            JOptionPane.showMessageDialog(this, "Valores Inválidos!", "Controle Empresa - Venda",
                JOptionPane.ERROR_MESSAGE);            
        }else{            
            if(vControl.qtdValido(jListProdutos.getSelectedIndex())){
                vControl.addProduto(jListProdutos.getSelectedIndex(), Integer.parseInt(jTxtQtd.getText()));
                jBtnCompra.setEnabled(true);
                atualizaCarrinho();
            }else
                JOptionPane.showMessageDialog(this, "Quantidade Inválida!", "Controle Empresa - Venda",
                JOptionPane.ERROR_MESSAGE);  
        }
    }//GEN-LAST:event_jBtnAdicionaCompraActionPerformed

    private void jBtnCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCompraActionPerformed
        vControl.cadastraVenda(clt);
        JOptionPane.showMessageDialog(this, "Compra efetuada!", "Controle Empresa - Venda",
                JOptionPane.INFORMATION_MESSAGE);
        dispose();
    }//GEN-LAST:event_jBtnCompraActionPerformed

    private void atualizaCarrinho(){
        jListCompra.setModel(modelCarrinho);
        modelCarrinho.clear();
        for(Produto pd : vControl.getProdutos())
            modelCarrinho.addElement(pd.getNome());
        jListCompra.setModel(modelCarrinho);
    }
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrmVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrmVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrmVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrmVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrmVenda().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAdicionaCompra;
    private javax.swing.JButton jBtnCompra;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLblListaProdutos;
    private javax.swing.JLabel jLblNomeCliente;
    private javax.swing.JList jListCompra;
    private javax.swing.JList jListProdutos;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTxtQtd;
    // End of variables declaration//GEN-END:variables
}
